﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.IO;

namespace QuanLyBepAnTuThien
{
    class connect
    {
        public string Server;
        public string Database;
        public string UserName;
        public string Password;
        public string Quyen;
        SqlConnection conn;
        public connect()
        {
            //StreamReader reader = new StreamReader("connectinfo.txt");
            //this.Server = reader.ReadLine().Split(':')[1];
            //this.Database = reader.ReadLine().Split(':')[1];
            //this.UserName = reader.ReadLine().Split(':')[1];
            //this.Password = reader.ReadLine().Split(':')[1];
            //this.Quyen = reader.ReadLine().Split(':')[1];
            //reader.Close();
        }
        public SqlConnection ketnoi()
        {
            StreamReader reader = new StreamReader("connectinfo.txt");
            this.Server = reader.ReadLine().Split(':')[1];
            this.Database = reader.ReadLine().Split(':')[1];
            this.UserName = reader.ReadLine().Split(':')[1];
            this.Password = reader.ReadLine().Split(':')[1];
            this.Quyen = reader.ReadLine().Split(':')[1];
            reader.Close();
            if (this.Quyen == "1")
            {
                conn = new SqlConnection(@"Data Source=" + this.Server + ";Initial Catalog=" + this.Database + ";Integrated Security=True");
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                else
                {
                    conn.Close();
                }
                return conn;
            }
            else
            {
                conn = new SqlConnection(@"Data Source=" + this.Server + ";Initial Catalog=" + this.Database + ";User ID=" + this.UserName + ";Password=" + this.Password + "");
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                else
                {
                    conn.Close();
                }
                return conn;
            }            
        }
        public DataTable getDataTable(string procedure)
        {
            SqlCommand cmd = new SqlCommand(procedure, ketnoi());
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }
        public String md5(string txt)
        {
            string str = "";
            Byte[] buffer = System.Text.Encoding.UTF8.GetBytes(txt);
            System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            buffer = md5.ComputeHash(buffer);
            foreach (Byte b in buffer)
            {
                str += b.ToString("X2");
            }
            return str;
        }
        public string GetTopDataCell(string nameTable, string nameSelectColumn, string orderByColumn)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = ketnoi();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT * FROM " + nameTable + " ORDER BY " + orderByColumn + " DESC";
            DataTable dtb = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            da.Fill(dtb);
            return dtb.Rows[0][nameSelectColumn].ToString();
        }

        public string NextID(string lastID, string prefixID)
        {
            int nextID = int.Parse(lastID.Remove(0, prefixID.Length)) + 1;
            int lengthNumerID = lastID.Length - prefixID.Length;
            string zeroNumber = "";
            for (int i = 1; i <= lengthNumerID; i++)
            {
                if (nextID < Math.Pow(10, i))
                {
                    for (int j = 1; j <= lengthNumerID - i; i++)
                    {
                        zeroNumber += "0";
                    }
                    return prefixID + zeroNumber + nextID.ToString();
                }
            }
            return prefixID + nextID;
        }
        public string ToProperName(string InputString)
        {
            if (InputString == "")
                return "";
            string ToProper = "";
            string[] words = InputString.Split(' ');
            foreach (string word in words)
            {
                char[] letters = word.ToCharArray();
                string tmpStr = letters[0].ToString().ToUpper();
                for (int i = 1; i < letters.Length; i++)
                    tmpStr += letters[i].ToString().ToLower();
                ToProper = ToProper + tmpStr + " ";
            }
            return ToProper.Trim();
        }
        public string DocSo(double number)
        {
            string str = "";
            string s = number.ToString("#");
            string[] so = new string[] { "Không", "Một", "Hai", "Ba", "Bốn", "Năm", "Sáu", "Bảy", "Tám", "Chín" };
            string[] hang = new string[] { "", "Nghìn", "Triệu", "Tỷ" };
            int i, j, donvi, chuc, tram;

            bool booAm = false;
            decimal decS = 0;
            //Tung addnew
            try
            {
                decS = Convert.ToDecimal(s.ToString());
            }
            catch
            {
            }
            if (decS < 0)
            {
                decS = -decS;
                s = decS.ToString();
                booAm = true;
            }
            i = s.Length;
            if (i == 0)
                str = so[0] + str;
            else
            {
                j = 0;
                while (i > 0)
                {
                    donvi = Convert.ToInt32(s.Substring(i - 1, 1));
                    i--;
                    if (i > 0)
                        chuc = Convert.ToInt32(s.Substring(i - 1, 1));
                    else
                        chuc = -1;
                    i--;
                    if (i > 0)
                        tram = Convert.ToInt32(s.Substring(i - 1, 1));
                    else
                        tram = -1;
                    i--;
                    if ((donvi > 0) || (chuc > 0) || (tram > 0) || (j == 3))
                        str = hang[j] + str;
                    j++;
                    if (j > 3) j = 1;
                    if ((donvi == 1) && (chuc > 1))
                        str = "Một " + str;
                    else
                    {
                        if ((donvi == 5) && (chuc > 0))
                            str = "Lăm " + str;
                        else if (donvi > 0)
                            str = so[donvi] + " " + str;
                    }
                    if (chuc < 0)
                        break;
                    else
                    {
                        if ((chuc == 0) && (donvi > 0)) str = "Lẻ " + str;
                        if (chuc == 1) str = "Mười " + str;
                        if (chuc > 1) str = so[chuc] + " Mươi " + str;
                    }
                    if (tram < 0) break;
                    else
                    {
                        if ((tram > 0) || (chuc > 0) || (donvi > 0)) str = so[tram] + " Trăm " + str;
                    }
                    str = " " + str;
                }
            }
            if (booAm) str = "Âm " + str;
            return str = str + "Đồng Chẵn";
        }
    
    }

}
